package cn.itigger.generate.core;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.alibaba.fastjson.JSON;

/**
 * 代码生成
 * 
 * @author Administrator
 *
 */

public class CodeUtil {
	private JFrame frame;
	private JPanel outer;
	private List<JText> fieldList = new ArrayList<CodeUtil.JText>();
	private Map<String, JCheckBox> cfg = new HashMap<String, JCheckBox>();
	private JButton create;
	private Integer height = 0;

	public void addCheckBox(String label, String key, int x, int y,
			int width, int height,boolean checked) {
		JCheckBox jCheckBox = new JCheckBox(label, checked);
		outer.add(jCheckBox);
		jCheckBox.setBounds(x, y, width, height);
		cfg.put(key,jCheckBox);
		
		if(key.equals("all")){
			jCheckBox.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					JCheckBox source = (JCheckBox) e.getSource();
					for(Entry<String, JCheckBox> entry : cfg.entrySet()){
						entry.getValue().setSelected(source.isSelected());
					}
				}
			});
		}
		
	}
	
	class JText{
		private String key;
		private String desc;
		private boolean notNull;
		private JTextField field;
		
		public JText(){}
		
		public JText(String key, String desc, JTextField field) {
			super();
			this.key = key;
			this.desc = desc;
			this.field = field;
			this.notNull = true;
		}
		
		public JText(String key, String desc, JTextField field,boolean notNull) {
			super();
			this.key = key;
			this.desc = desc;
			this.field = field;
			this.notNull = notNull;
		}
		
		public String getKey() {
			return key;
		}
		public void setKey(String key) {
			this.key = key;
		}
		public String getDesc() {
			return desc;
		}
		public void setDesc(String desc) {
			this.desc = desc;
		}
		public JTextField getField() {
			return field;
		}
		public void setField(JTextField field) {
			this.field = field;
		}

		public boolean isNotNull() {
			return notNull;
		}

		public void setNotNull(boolean notNull) {
			this.notNull = notNull;
		}
		
		
	}
	
	private void addInput(String key,String desc,String value,boolean notNull){
		JLabel label = new JLabel(desc+":");
		outer.add(label);
		label.setBounds(44, 52+(fieldList.size()*30), 150, 15);
		JTextField field = new JTextField(value);
		outer.add(field);
		field.setBounds(164, 50+(fieldList.size()*30), 250, 22);
		fieldList.add(new JText(key, desc, field,notNull));
		height = 52+(fieldList.size()*30);
	}

	public void init(Map<String, String> config) {
		
		String projectPath = CodeGenerateFactory.getProjectPath();
		
		String workSpace = projectPath.substring(0,projectPath.lastIndexOf("/"));
		
		String projectName = projectPath.substring(projectPath.lastIndexOf("/")+1,projectPath.length());
		
		try {
			frame = new JFrame();
			frame.setTitle("代码生成器：");
			outer = new JPanel();
			frame.getContentPane().add(outer);
			outer.setBounds(41, 34, 313, 194);
			outer.setBorder(BorderFactory.createTitledBorder("配置数据:"));
			outer.setLayout(null);
			
			addInput("projectName","项目名称",projectName,true);
			
			addInput("table","表名","user",true);
			
			addInput("workSpace","工作空间",workSpace,true);
			
			addInput("basePackage","包名",config!=null&&config.get("basePackage")!=null?config.get("basePackage"):"cn.itigger."+projectName,true);
			
			addInput("prefix","接口访问前缀",config!=null&&config.get("prefix")!=null?config.get("prefix"):"",false);
			
			addInput("webPath","页面生成路径",config!=null&&config.get("webPath")!=null?config.get("webPath"):"webapp\\page\\",false);
			
			height += 30;

			addCheckBox("生成entity", "entity", 44, height, 85, 22, false);

			addCheckBox("生成mapper类", "mapper", 144, height, 110, 22, false);

			addCheckBox("生成mapper xml", "mapper_xml", 274, height, 120, 22, false);
			
			height += 30;

			addCheckBox("生成service", "service", 44, height, 100, 22, false);

			addCheckBox("生成controller", "controller", 144, height, 110, 22, false);
			
			addCheckBox("生成页面", "page", 274, height, 110, 22, false);
			
			height += 30;
			
			addCheckBox("以上全选", "all", 44, height, 110, 22, false);

			height += 50;
			create = new JButton("create code");
			outer.add(create);
			create.setBounds(119, height, 120, 22);

			create.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					Map<String, String> data = new HashMap<String, String>();
					
					for(JText j : fieldList){
						if(j.isNotNull()&&j.getField().getText().trim().equals("")){
							JOptionPane.showMessageDialog(null, "请填写"+j.getDesc()+"！");
							return;
						}
						data.put(j.getKey(), j.getField().getText().trim());
					}
					CodeGenerateFactory.codeGenerate(data, cfg);
				}
			});
			frame.setSize(750, 450);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setLocationRelativeTo(null);
			frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
